<?php
namespace Home\Controller;
use Think\Controller;
class DataapiController extends Controller {
	public function __call($function_name, $args){
        echo json_encode(array());die;
    }
	
    //大众点评-运动
	public function dianping_sport(){
		$page = I('get.page',1,'intval');
        $pagesize = I('get.size',20,'intval');
        $category = I('get.category','');
        $city = I('get.city',null);
        $district = I('get.district',null);
        if(empty($district) || empty($city)) {
            echo json_encode( array('status'=>1003,'msg'=>'缺少必要参数',data=>null));exit;
        }

        $table = "dianping_sport";
        $sql = "select city,district,categories as category,address,avg_price,decoration_score,service_score,product_score,review_count,telephone from ".$table." where 1=1 ";

        $sql .= " and city = '".$city."' ";
        $sql .= " and district = '".$district."' ";

		if(!empty($category)){
			$sql.=" and categories like '%".$category."%' ";
		}
        
		$sql .= " order by cid ";
		$sql .= " limit ".$pagesize." offset ".($page-1)*$pagesize; 
		
		$pg_connect = $this->conncetPgsql();
        $result = pg_query($pg_connect,$sql);
        pg_close($pg_connect);
        
        $data = array();
        while($list = pg_fetch_object($result)){
            array_push($data, $list);
        }
        
        echo json_encode(array('status'=>0,'msg'=>'ok','data'=>$data),JSON_UNESCAPED_UNICODE);
	}

    //赶集二手车
    public function ershouche(){
        $page = I('get.page',1,'intval');
        $pagesize = I('get.size',20,'intval');
        $city = I('get.city',null);
        $title = I('get.title',null);
        $price = I('get.price',null);//价格，支持区间，格式为“10000-20000”
        $mileage = I('get.mileage',0,'intval');//里程
        $displacement = I('get.displacement',0,'intval');//排量
        $gearbox = I('get.gearbox',0,'intval');//变速箱

        $table = "ershouche";
        $sql = "select city,mileage,accident,title,price,msrp,phone,ontime,strong_insurance,yearly_inspection,descri,air_displacement as displacement,gearbox,color from ".$table." where 1=1 ";

        if(!empty(trim($city))){
            $sql .= " and city = '".$city."' ";    
        }
        if(!empty( trim($title) )){
            $sql .= " and title  like '%".$title."%' ";    
        }
        if(!empty( trim($price) )){
            if(stripos($price,'-')===false){
                $sql .= " and price  <= ".(int)$price." ";
            }else{
                $price_range_array = explode("-", trim($price));
                if(!empty($price_range_array[0]) && is_numeric($price_range_array[0])) $sql .= " and price  >= ".(int)$price_range_array[0]." ";
                if(!empty($price_range_array[1]) && is_numeric($price_range_array[1])) $sql .= " and price  <= ".(int)$price_range_array[1]." ";
            }
            
        }
        if(is_numeric($mileage) && $mileage){
            switch($mileage){
                case 1://5000公里以下
                    $sql .= " and mileage  <= 0.5 ";
                    break;
                case 2://1w公里以下
                    $sql .= " and mileage  <1 ";
                    break;
                case 3://1-3w公里
                    $sql .= " and mileage between 1 and 3 ";
                    break;
                case 4://3-5w公里
                    $sql .= " and mileage between 3 and 5 ";
                    break;
                case 5://5-8w公里
                    $sql .= " and mileage between 5 and 8 ";
                    break;
                case 6://8-10w公里
                    $sql .= " and mileage between 8 and 10 ";
                    break;
                case 7://10w公里以上
                //default:
                    $sql .= " and mileage > 10 ";
                    break;

            }
        }
        if(is_numeric($displacement) && $displacement){
            switch($displacement){
                case 1://1.0以下
                    $sql .= " and air_displacement  < 1.0 ";
                    break;
                case 2://1.0-1.6升
                    $sql .= " and air_displacement  between 1.0 and 1.6 ";
                    break;
                case 3://1.6-2.0升
                    $sql .= " and air_displacement between 1.6 and 2.0 ";
                    break;
                case 4://2.0-3.0升
                    $sql .= " and air_displacement between 2.0 and 3.0 ";
                    break;
                case 5://3.0以上
                //default:
                    $sql .= " and air_displacement >3.0 ";
                    break;
            }
        }
        if(is_numeric($gearbox) && $gearbox){
            switch($gearbox){
                case 1://手动
                    $sql .= " and gearbox ='手动' ";
                    break;
                case 2://自动
                    $sql .= " and gearbox = '自动' ";
                    break;    
            }
            
        }
        
        $sql .= " order by cid ";
        $sql .= " limit ".$pagesize." offset ".($page-1)*$pagesize; 
        
        $pg_connect = $this->conncetPgsql();
        $result = pg_query($pg_connect,$sql);
        pg_close($pg_connect);
        
        $data = array();
        while($list = pg_fetch_object($result)){
            array_push($data, $list);
        }
        
        echo json_encode(array('status'=>0,'msg'=>'ok','data'=>$data),JSON_UNESCAPED_UNICODE);
    }

    //链接pgsql(dataex爬虫数据)
    protected function conncetPgsql(){
        $conn_string  =  "host=".C(DATAEXGP_HOST)." port=".C(DATAEXGP_PORT)." dbname=".C(DATAEXGP_DB_NAME)." user=".C(DATAEXGP_DB_USER)." password=".C(DATAEXGP_DB_PWD); 
        $dbconn = pg_connect($conn_string);
        
        if ($dbconn){
            return $dbconn;
        } else {
            pg_close($dbconn);
            $this->error('链接pg库失败');
        }
    }
}